package com.hnkywl.jip.action;

import com.hnkywl.jip.dao.VoucherType;
import com.hnkywl.jip.interceptor.AuthInterceptor;
import com.hnkywl.jip.service.CompanyService;
import com.hnkywl.jip.service.OtherVoucherService;
import com.hnkywl.jip.service.ReceiptPaymentService;
import com.hnkywl.jip.service.VoucherService;
import com.hnkywl.jip.utils.UUIDGenerator;
import com.hnkywl.jip.utils.XmlParse;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-2-26
 * Time: 下午4:22
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/other_voucher")
public class OtherVoucherAction extends SimpleActionSupport {
    private Integer type;
    private Integer customerId;
    private Integer billNumber;
    private Integer voucherId;
    private List<Object[]> companyList;

    private Map<String, String> result = new HashMap<String, String>();
    private Map userInfo;
    private XmlParse xmlParse;
    private ReceiptPaymentService receiptPaymentService;
    private CompanyService companyService;
    private Integer status;
    private Map<String, Object> voucher;

    private Integer id;

    private String operator;

    private Date startTime;

    private Date endTime;

    private Date systemDate;

    private Integer bill;

    private Integer companyId;
    private Integer attachment;

    private Integer limit;
    private Integer start;

    private Page<Object[]> gridResult;

    private OtherVoucherService otherVoucherService;

    private VoucherService voucherService;

    @Action("search")
    public String search() {
        endTime = new Date();
        startTime = new Date();
        startTime.setDate(1);
        return SUCCESS;
    }

    @Action("grid_json")
    public String gridJson() {
        gridResult = otherVoucherService.findOtherVoucher(startTime, endTime, companyId, billNumber,
                status, voucherId, operator,limit, start);
        return SUCCESS;
    }
    @Action("voucher_json")
    public String voucherJson() {
        systemDate = new Date();
        userInfo = (HashMap<String, Object>) this.session.get(AuthInterceptor.SESSION_KEY);
        id=26809;
        voucher = otherVoucherService.getVoucher(id, null);
        Map<String, Object> voucherMaster = otherVoucherService.getVoucherMaster(id, attachment, null, null, null,userInfo);
        voucher.put("master", voucherMaster);
        return SUCCESS;
    }

    @Action(value = "generate_voucher_json",
            results = {@Result(name = "success", location = "/WEB-INF/content/other_voucher/response_json.ftl")})
    public String generateVoucher() {
        try {
            String uuid = UUIDGenerator.getUUID();
            Integer year = GregorianCalendar.getInstance().get(Calendar.YEAR);
            Integer mouth = GregorianCalendar.getInstance().get(Calendar.MONTH);
            Boolean begin = voucherService.beginGenerateVoucher(id, VoucherType.OTHER_VOUCHER);
            userInfo = (HashMap<String, Object>) this.session.get(AuthInterceptor.SESSION_KEY);
            if (!begin)
                throw new RuntimeException("其他用户已经开始导入该凭证，请选择其他凭证进行导入!");
            //凭证分录
            voucher = otherVoucherService.getVoucher(id, uuid);
            //凭证头
            Map<String, Object> msgheader = voucherService.getMsgHeader(userInfo);
            voucher.put("msgheader", msgheader);
            //凭证主表信息
            Map<String, Object> voucherMaster =
                    otherVoucherService.getVoucherMaster(id, attachment, uuid, year.toString(), mouth.toString(),userInfo);
            voucher.put("master", voucherMaster);
            String r = voucherService.generateVoucher(voucher);
            result = xmlParse.getContent(r);
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                String vchrid = result.get(XmlParse.VCHRID);
                String vchrnum = result.get(XmlParse.VCHRNUM);
                Boolean success = voucherService.generateVoucherSuccess(uuid, vchrid, vchrnum, year, id, VoucherType.OTHER_VOUCHER);
                if (!success)
                    throw new RuntimeException("凭证导入成功，但凭证信息回写失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        return SUCCESS;
    }


    @Action(value = "revoke_voucher_json",
            results = {@Result(name = "success", location = "/WEB-INF/content/other_voucher/response_json.ftl")})
    public String revokeVoucher() {
        try {
            userInfo = (HashMap<String, Object>) this.session.get(AuthInterceptor.SESSION_KEY);
            voucherService.beginRevokeVoucher(id, VoucherType.RECEIPT_PAYMENT);
            Map<String, Object> msgheader = voucherService.getMsgHeader(userInfo);
            Map<String, Object> revokeInfo = receiptPaymentService.getRevokeInfo(id);
            Map<String, Object> info = new HashMap<String, Object>();
            info.put("msgheader", msgheader);
            info.put("revokeinfo", revokeInfo);
            String r = voucherService.revokeVoucher(info);
            result = xmlParse.getContent(r);
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                voucherService.revokeVoucherSuccess(id, VoucherType.RECEIPT_PAYMENT);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        return SUCCESS;
    }

    @Autowired
    public void setOtherVoucherService(OtherVoucherService otherVoucherService) {
        this.otherVoucherService = otherVoucherService;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }



    public String getOperator() {
        return operator;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Integer getBill() {
        return bill;
    }

    public void setBill(Integer bill) {
        this.bill = bill;
    }

    public Integer getCompanyId() {
        return companyId;
    }

    public void setCompanyId(Integer companyId) {
        this.companyId = companyId;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Integer getStart() {
        return start;
    }

    public void setStart(Integer start) {
        this.start = start;
    }

    public Page<Object[]> getGridResult() {
        return gridResult;
    }

    public void setGridResult(Page<Object[]> gridResult) {
        this.gridResult = gridResult;
    }

    public Date getSystemDate() {
        return systemDate;
    }

    public void setSystemDate(Date systemDate) {
        this.systemDate = systemDate;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Integer getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }


    public Integer getBillNumber() {
        return billNumber;
    }

    public void setBillNumber(Integer billNumber) {
        this.billNumber = billNumber;
    }

    public Integer getVoucherId() {
        return voucherId;
    }

    public void setVoucherId(Integer voucherId) {
        this.voucherId = voucherId;
    }

    public List<Object[]> getCompanyList() {
        return companyList;
    }

    public void setCompanyList(List<Object[]> companyList) {
        this.companyList = companyList;
    }

    public Map<String, String> getResult() {
        return result;
    }

    public void setResult(Map<String, String> result) {
        this.result = result;
    }

    public XmlParse getXmlParse() {
        return xmlParse;
    }

    public void setXmlParse(XmlParse xmlParse) {
        this.xmlParse = xmlParse;
    }

    public Map<String, Object> getVoucher() {
        return voucher;
    }

    public void setVoucher(Map<String, Object> voucher) {
        this.voucher = voucher;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getAttachment() {
        return attachment;
    }

    public void setAttachment(Integer attachment) {
        this.attachment = attachment;
    }

    @Autowired

    public void setVoucherService(VoucherService voucherService) {
        this.voucherService = voucherService;
    }
    @Autowired
    public void setReceiptPaymentService(ReceiptPaymentService receiptPaymentService) {
        this.receiptPaymentService = receiptPaymentService;
    }
    @Autowired
    public void setCompanyService(CompanyService companyService) {
        this.companyService = companyService;
    }




}
